package com.tech.connect.util;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ksy.common.utils.LogTool;
import com.ksy.common.utils.ThreadManager;
import com.tech.connect.api.BaseEntityOb;
import com.tech.connect.api.HttpUtils;
import com.tech.connect.model.ItemCat;
import com.tech.connect.model.ItemCatSub;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CatUtils {
    private Context context;
    private Map<String, ItemCat> catMap = new HashMap<>();
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            HttpUtils.allCat(new BaseEntityOb<List<Map<String, String>>>() {
                @Override
                public void onDataDeal(boolean isSuccess, List<Map<String, String>> mapList, String msg) {
                    if (isSuccess && mapList != null) {
                        catMap.clear();
                        Gson gson = new Gson();
                        for (Map<String, String> map : mapList) {
                            String id = map.get("id");
                            String code = map.get("code");
                            String name = map.get("name");
                            String contentJson = map.get("content");
                            ItemCat itemCat = new ItemCat();
                            itemCat.id = id;
                            itemCat.code = code;
                            itemCat.name = name;
                            itemCat.catList = new ArrayList<>();
                            if (!TextUtils.isEmpty(contentJson)) {
                                List<ItemCatSub> subList = gson.fromJson(contentJson, new TypeToken<List<ItemCatSub>>() {
                                }.getType());
                                if (subList != null) {
                                    itemCat.catList.addAll(subList);
                                }
                            }
                            catMap.put(itemCat.code, itemCat);
                        }
                        LogTool.e(mapList.size());
                    }
                }
            });
        }
    };
    private static CatUtils instance;

    private CatUtils() {
    }

    public static CatUtils getInstance() {
        if (instance == null) {
            synchronized (CatUtils.class) {
                if (instance == null) {
                    instance = new CatUtils();
                    return instance;
                }
            }
        }
        return instance;
    }

    public void init(@NonNull Context context) {
        this.context = context.getApplicationContext();
        ThreadManager.getNormalPool().execute(runnable);
    }

    public List<ItemCatSub> getCatListByCode(String code) {
        if (catMap.containsKey(code)) {
            ItemCat itemCat = catMap.get(code);
            return itemCat.catList;
        }
        return null;
    }
}
